// pages/list/zulinlist.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.globalData.imgurl,
    showFilter:false,
    tabs: [
      {
        name: '全部',
        selected: 1,
        event: 'getall'
      }, {
        name: '销量',
        selected: 0,
        event: 'orderByQuentity'
      }, {
        name: '价格',
        selected: 0,
        event: 'showfilter'
      }
    ],
    filter: [
      {
        title: '价格',
        options: [
          {
            name: '最低价',
          },{
            name: '最高价',
          },
        ]
      },
      // {
      //   title: '品牌',
      //   options: [
      //     {
      //       name: '品牌',
      //     }, {
      //       name: '品牌',
      //     }, {
      //       name: '品牌',
      //     }, {
      //       name: '品牌',
      //     }, {
      //       name: '品牌',
      //     }, {
      //       name: '品牌',
      //     }, {
      //       name: '品牌',
      //     }, {
      //       name: '品牌',
      //     }
      //   ]
      // }
    ]
  },
  // 头部tab
  switchTab: function (e) {
    var o = e.currentTarget.dataset;
    var tabs = this.data.tabs;
    for (var i = 0; i < tabs.length; i++) {
      tabs[i].selected = 0;
    }
    tabs[o.index].selected = 1;
    this.setData({
      tabs: tabs
    })
  },
  // 全部商品
  getall: function () {
    this.getGoodsList({cid:this.data.cid})
  },
  // 数量排序
  orderByQuentity: function () {
    this.listorder(0)
    
  },
  listorder:function(ordertype){
    var that = this
    wx.request({
      url: app.globalData.url + 'product_zi?type=' + ordertype+'&cid=' + this.data.cid,
      success: res => {
        var goodlist = res.data
        for (var i = 0; i < goodlist.length; i++) {
          if (goodlist[i].title.length > 32) {
            goodlist[i].title_substr = goodlist[i].title.substr(0, 32)
          } else {
            goodlist[i].title_substr = goodlist[i].title
          }
        }
        that.setData({
          goodlist: goodlist
        })
      }

    })
  },
  // 筛选条件
  showfilter: function (e) {
    var filter = this.data.filter;
    this.setData({
      showFilter: !this.data.showFilter,
    })
  },
  select:function(e){
    var o = e.currentTarget.dataset;
    var filter = this.data.filter;
    var s = filter[o.opidx].options[o.cindex].selected
    for (var i = 0; i < filter[o.opidx].options.length; i++){
      filter[o.opidx].options[i].selected = 0
    }
    filter[o.opidx].options[o.cindex].selected = (s?0:1)
    this.setData({
      filter: filter
    })  
  },
  // 提交筛选条件  
  summit:function(){
    var filter = this.data.filter;
    var priceorder = filter[0]
    // 价格排序
    for (var i = 0; i < priceorder.options.length; i++ ){
      if (priceorder.options[i].selected){
        this.listorder(i+1)
      }
    }
    this.setData({
      showFilter: false,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    console.log(e)
    this.setData({
      cid:e.cid
    })
    this.getGoodsList(e)
  },
  // 加载子分类
  getGoodsList(e){
    var that = this
    wx.request({
      url: app.globalData.url +'product_zi?cid=' +e.cid,
      success:function(res){
        console.log(res)
        var goodlist = res.data
        console.log(typeof goodlist)
        if(goodlist){
          for (var i = 0; i < goodlist.length; i++) {
            if (goodlist[i].title.length > 32) {
              goodlist[i].title_substr = goodlist[i].title.substr(0, 32)
            } else {
              goodlist[i].title_substr = goodlist[i].title
            }
          }
        }
        that.setData({
          goodlist:goodlist
        })
      }
    })
  },
  toDetail:function(e){
    var o = e.currentTarget.dataset
    console.log(o)
    wx.navigateTo({
      url: '/pages/list/detail/detail?id='+o.id,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})